﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace ncs.lingtian
{
    public partial class LingTianMod
    {

        private bool isPanelVisible = false;
        private string lingtiansuduInput = "";
        private string julingsuduInput = "";
        private string lingtiancuishengsuduInput = "";
        private string pingziInput = "";
        private string lingyanxiuliansuduInput = "";
        private string julingzhenxiuliansuduInput = "";
        private string ganWuTimeInput = "";
        private string lingWuOrTuPoTimeInput = "";
        private string lingGanInput = "";
        private string lianqiCostTimeInput = ""; 
        private string menPaiHaoGanInput = ""; 



        #region IMGUI
        // 添加输入框字符串变量


        void OnGUI()
        {
            var top = 250;
            var left = 2;
            var width = 300;
            var height = 800;

            if (!isPanelVisible) return;

            // 创建配置窗口
            Rect windowRect = new Rect(left, top, width, height);
            windowRect = GUI.Window(0, windowRect, DrawConfigWindow, "灵田增强配置");

            // 添加关闭按钮
            if (GUI.Button(new Rect(windowRect.width + windowRect.x, windowRect.y, 80, 25), "关闭"))
            {
                isPanelVisible = false;
            }
        }

        private void DrawConfigWindow(int windowID)
        {
            GUILayout.BeginVertical();

            // 灵田配置部分
            GUILayout.Label("灵眼", GUI.skin.label);

            #region 催生速度
            GUILayout.BeginHorizontal();
            GUILayout.Label($"催生速度({lingtiansudu.Value}):");
            lingtiansuduInput = GUILayout.TextField(lingtiansuduInput, GUILayout.Width(120));
            GUILayout.EndHorizontal(); 
            #endregion

            #region 修炼速度
            GUILayout.BeginHorizontal();
            GUILayout.Label($"修炼速度{lingyanxiuliansudu.Value}):");
            lingyanxiuliansuduInput = GUILayout.TextField(lingyanxiuliansuduInput, GUILayout.Width(120));
            GUILayout.EndHorizontal(); 
            #endregion


            // 瓶子配置部分
            GUILayout.Space(5);
            GUILayout.Label("小绿瓶", GUI.skin.label);

            #region 灵气倍率
            GUILayout.BeginHorizontal();
            GUILayout.Label($"灵气倍率({pingzicuishengsudu.Value}):");
            lingtiancuishengsuduInput = GUILayout.TextField(lingtiancuishengsuduInput, GUILayout.Width(120));
            GUILayout.EndHorizontal(); 
            #endregion

            #region 灵石倍率
            GUILayout.BeginHorizontal();
            GUILayout.Label($"灵石倍率({pingzi.Value}):");
            pingziInput = GUILayout.TextField(pingziInput, GUILayout.Width(120));
            GUILayout.EndHorizontal(); 
            #endregion


            // 聚灵阵
            GUILayout.Space(5);
            GUILayout.Label("聚灵阵", GUI.skin.label);

            #region 聚灵倍率
            GUILayout.BeginHorizontal();
            GUILayout.Label($"灵眼倍率({julingsudu.Value}):");
            julingsuduInput = GUILayout.TextField(julingsuduInput, GUILayout.Width(120));
            GUILayout.EndHorizontal(); 
            #endregion

            #region 聚灵倍率
            GUILayout.BeginHorizontal();
            GUILayout.Label($"聚灵倍率({julingzhenxiuliansudu.Value}):");
            julingzhenxiuliansuduInput = GUILayout.TextField(julingzhenxiuliansuduInput, GUILayout.Width(120));
            GUILayout.EndHorizontal(); 
            #endregion

            // 感悟配置部分
            GUILayout.Space(5);
            GUILayout.Label("感悟/灵感", GUI.skin.label);

            #region 感悟速度
            GUILayout.BeginHorizontal();
            GUILayout.Label($"感悟速度({ganWuTime.Value}):");
            ganWuTimeInput = GUILayout.TextField(ganWuTimeInput, GUILayout.Width(120));
            GUILayout.EndHorizontal(); 
            #endregion

            #region 领悟速度
            GUILayout.BeginHorizontal();
            GUILayout.Label($"领悟速度({lingWuOrTuPoTime.Value}):");
            lingWuOrTuPoTimeInput = GUILayout.TextField(lingWuOrTuPoTimeInput, GUILayout.Width(120));
            GUILayout.EndHorizontal(); 
            #endregion

            #region 灵感恢复速度
            GUILayout.BeginHorizontal();
            GUILayout.Label($"灵感恢复速度({lingGan.Value}):");
            lingGanInput = GUILayout.TextField(lingGanInput, GUILayout.Width(120));
            GUILayout.EndHorizontal();
            #endregion

            #region 炼器速度
            GUILayout.BeginHorizontal();
            GUILayout.Label($"炼器速度({lianqiCostTime.Value}):");
            lianqiCostTimeInput = GUILayout.TextField(lianqiCostTimeInput, GUILayout.Width(120));
            GUILayout.EndHorizontal();
            #endregion

            #region 门派声望
            GUILayout.BeginHorizontal();
            GUILayout.Label($"门派声望({menPaiHaoGan.Value}):");
            menPaiHaoGanInput = GUILayout.TextField(menPaiHaoGanInput, GUILayout.Width(120));
            GUILayout.EndHorizontal();
            #endregion

            // 操作按钮
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("保存配置", GUILayout.Height(30)))
            {
                try
                {
                    if (!string.IsNullOrEmpty(lingtiansuduInput))
                        lingtiansudu.Value = int.Parse(lingtiansuduInput);
                    if (!string.IsNullOrEmpty(julingsuduInput))
                        julingsudu.Value = int.Parse(julingsuduInput);
                    if (!string.IsNullOrEmpty(lingtiancuishengsuduInput))
                        pingzicuishengsudu.Value = int.Parse(lingtiancuishengsuduInput);
                    if (!string.IsNullOrEmpty(pingziInput))
                        pingzi.Value = int.Parse(pingziInput);
                    if (!string.IsNullOrEmpty(lingyanxiuliansuduInput))
                        lingyanxiuliansudu.Value = int.Parse(lingyanxiuliansuduInput);
                    if (!string.IsNullOrEmpty(julingzhenxiuliansuduInput))
                        julingzhenxiuliansudu.Value = int.Parse(julingzhenxiuliansuduInput);
                    if (!string.IsNullOrEmpty(ganWuTimeInput))
                        ganWuTime.Value = int.Parse(ganWuTimeInput);
                    if (!string.IsNullOrEmpty(lingWuOrTuPoTimeInput))
                        lingWuOrTuPoTime.Value = int.Parse(lingWuOrTuPoTimeInput);
                    if (!string.IsNullOrEmpty(lingGanInput))
                        lingGan.Value = int.Parse(lingGanInput);
                    if (!string.IsNullOrEmpty(lianqiCostTimeInput))
                        lianqiCostTime.Value = int.Parse(lianqiCostTimeInput);
                    if (!string.IsNullOrEmpty(menPaiHaoGanInput))
                        menPaiHaoGan.Value = int.Parse(menPaiHaoGanInput);
                }
                catch (FormatException)
                {
                    Logger.LogError("输入值格式错误，请输入有效的数字");
                }
                Config.Save();
                DFLingYanLevel_InitDataDict_Postfix();
                DFZhenYanLevel_InitDataDict_Postfix();
                Logger.LogInfo("配置已保存");
            }

            if (GUILayout.Button("重置默认", GUILayout.Height(30)))
            {
                ResetToDefaults();
                // 清空输入框
                ClearInputFields();
                DFLingYanLevel_InitDataDict_Postfix();
                DFZhenYanLevel_InitDataDict_Postfix();
            }
            GUILayout.EndHorizontal();

            GUILayout.Space(10);

            // 状态信息
            GUIStyle wrappedLabelStyle = new GUIStyle(GUI.skin.label);
            wrappedLabelStyle.wordWrap = true;
            GUILayout.Label($"说明");
            GUILayout.Label($"草药生长速度=灵田灵力+瓶子灵力\n灵田灵力=灵眼灵力（和品级有关）+聚灵阵灵力（和等级有关）\n聚灵阵灵力=聚灵阵灵力+瓶子灵力\n上面所有的数都是整数，不能带小数点，输入错误会报错。\n门派好感度对应声望，对任务无效，只对提交草药类的有效。", wrappedLabelStyle);  

            GUILayout.EndVertical(); 
        }

        private void ResetToDefaults()
        {
            lingtiansudu.Value = 1;
            julingsudu.Value = 1;
            pingzicuishengsudu.Value = 1;
            pingzi.Value = 1;
            lingyanxiuliansudu.Value = 1;
            julingzhenxiuliansudu.Value = 1;
            ganWuTime.Value = 1;
            lingWuOrTuPoTime.Value = 1;
            lingGan.Value = 1;
            lianqiCostTime.Value = 1;
            menPaiHaoGan.Value = 1;
            Logger.LogInfo("配置已重置为默认值");
        }

        private void ClearInputFields()
        {
            lingtiansuduInput = "";
            julingsuduInput = "";
            lingtiancuishengsuduInput = "";
            pingziInput = "";
            lingyanxiuliansuduInput = "";
            julingzhenxiuliansuduInput = "";
            ganWuTimeInput = "";
            lingWuOrTuPoTimeInput = "";
            lingGanInput = "";
            lianqiCostTimeInput = "";
            menPaiHaoGanInput = "";
        }
        #endregion
    }
}
